import http from "../util/httpRequest.js"
import config from "../config.js"

// 随机生成10条测试数据
function genTestOrderList() {
	let list = []
	let statusList = [config.orderStatus.NotPay, config.orderStatus.WaitSend, config.orderStatus.WaitTake, config
		.orderStatus.Finished
	]
	let timestamp = new Date().getTime()
	let goodsAvatar = "https://img0.baidu.com/it/u=3074490370,3840344350&fm=26&fmt=auto"
	let randInt = function(min, max) {
		return min + Math.floor(Math.random() * max);
	}
	for (let i = 0; i < 30; i++) {
		timestamp += randInt(1, 3600 * 24 * 1000)
		let order = {
			orderNo: timestamp + randInt(1, 100000000),
			userId: 1,
			orderStatus: statusList[randInt(0, statusList.length)],
			wxPayTransactionId: "4200001338202112" + randInt(1, 100000000),
			takeType: 1,
			addressDetail: "四川农业大学2区15舍309",
			goodsList: [],
			refundCount: 0,
			totalPrice: randInt(100, 2000),
			payPrice: randInt(100, 2000),
			extraInfo: "extraInfo-sxxx",
			createTime: timestamp,
			payTime: timestamp,
			finishTime: "",
			userPhone: "17058179046",
			receiver: "陈亚如"
		}
		// 订单商品列表
		for (let i = 0; i < randInt(1, 3); i++) {
			let needRefund = order.orderStatus != config.orderStatus.NotPay && (randInt(1, 100) < 30)
			order.goodsList.push({
				goodsId: i,
				price: randInt(1, 10),
				count: randInt(1, 3),
				propertyMap: {
					"属性1": "xxx1",
					"属性2": "yyy1",
				},
				name: "goods-" + randInt(0, 5),
				image: goodsAvatar,
				refundNo: needRefund ? "refundNoxxx" : "", // 退款号（如果该商品要退款的话）
				refundReason: needRefund ? "库存不够" : "",
			})
			if (needRefund) order.refundCount++
		}
		if (order.refundCount === order.goodsList.length) order.orderStatus = config.orderStatus.Refunded
		list.push(order)
	}
	return list
}

let orderListTest = genTestOrderList()

// 用户订单列表
export function getOrderList(params) {
	params.path = "/order/list"
	params.testData = orderListTest
	http(params)
}

// 订单详情
export function getOrderDetail(params) {
	params.path = "/order/detail/" + params.orderNo
	params.testData = orderListTest.find(e => {
		return e.orderNo === orderNo
	})
	http(params)
}

// 查询订单状态
export function getOrderStatus(params) {
	params.path = "/order/status/" + params.orderNo
	params.testData = function() {
		let order = orderListTest.find(e => {
			return e.orderNo == params.orderNo
		})
		return order ? order.orderStatus : 0
	}()
	http(params)
}


// 创建订单
export function createOrder(params) {
	params.method = "POST"
	params.path = "/order"
	params.data = params.orderInfo
	params.testData = function() {
		let orderNo = new Date().getTime() + Math.floor(Math.random() * 100000000)
		let order = {
			...params.orderInfo,
			orderNo: orderNo,
			orderStatus: config.orderStatus.NotPay,
			payPrice: 0,
			createTime: new Date().getTime(),
		}
		orderListTest = [order].concat(orderListTest)
		return orderNo
	}
	params.showLoading = true
	http(params)
}

// 确认支付完成
export function confirmFinishPay(params) {
	params.method = "PUT"
	params.path = "/order/confirmPay/" + params.orderNo
	params.testData = function() {
		for (let i = 0; i < orderListTest.length; i++)
			if (orderListTest[i].orderNo === orderNo)
				orderListTest[i] = config.orderStatus.WaitSend
		return true
	}
	http(params)
}

// 取消并删除订单
export function cancelOrder(params) {
	params.method = "DELETE"
	params.path = "/order/cancel/" + params.orderNo
	params.testData = function() {
		for (let i = 0; i < orderListTest.length; i++)
			if (orderListTest[i].orderNo === params.orderNo)
				orderListTest.splice(i, 1)
		return true
	}
	http(params)
}


// 确认收货
export function confirmReceive(params) {
	params.method = "PUT"
	params.path = "/order/confirmReceive/" + params.orderNo
	params.testData = function() {
		for (let i = 0; i < orderListTest.length; i++)
			if (orderListTest[i].orderNo === params.orderNo)
				orderListTest[i].orderStatus = config.orderStatus.Finished
		return true
	}
	http(params)
}
